/**
 * FileName: FileHandleThreadPool.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-11-28 下午5:58:14
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-11-28 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.me.safebox.data.SafeboxDao;

import android.os.Handler;
import android.util.Log;

/**
 * @ClassName: FileHandleThreadPool
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-11-28 下午5:58:14
 * 
 */
public class FileHandleThreadPool<T extends Callable<IFuture>> {
	private final static String TAG = "FileHandleThreadPool";

	private static int CPU_NUM = Runtime.getRuntime().availableProcessors();

	private static int POOL_SIZE = 3;

	public final static int COUNTING = 7;

	public final static int TASK_FINISH = 8;

	public final static int INSERT_ERROR = 9;

	private int taskTotal = 0;

	public Handler mhandler = null;

	private List<Future<IFuture>> resultList = new ArrayList<Future<IFuture>>();

	private ExecutorService threadPool = Executors.newFixedThreadPool(1);

	private SafeboxDao safeboxDao = new SafeboxDao();

	public synchronized void setSuccessTaskNum() {
		mhandler.sendEmptyMessage(COUNTING);
		Log.i(TAG, threadPool.isShutdown() + "");
	}

	public synchronized void setFailedTaskNum() {
		mhandler.sendEmptyMessage(COUNTING);
	}

	private List<T> taskList = new ArrayList<T>();

	public int getTaskTotal() {
		return taskTotal;
	}

	public void setTaskList(T task) {
		taskList.add(task);
		taskTotal = taskList.size();
	}

	public void startDecrypt() {
		Future<IFuture> future = null;
		for (T task : taskList) {
			future = threadPool.submit(task);
			// 将任务执行结果存储到List中
			resultList.add(future);
		}
		for (Future<IFuture> fs : resultList) {
			DecryFuture df;
			try {
				df = (DecryFuture) fs.get();
				//Log.i("filetask", "file id:" + df.fileId + " | permission:" + df.deletePermisson);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
			}
			mhandler.sendEmptyMessage(COUNTING);
		}
		try {
			safeboxDao.batchDeleteDecryFileInfo(resultList);
			mhandler.sendEmptyMessage(TASK_FINISH);
		} catch (InterruptedException e) {
		} catch (ExecutionException e) {
		} finally {
			freelize();
		}
	}

	public void startEncrypt() {
		Future<IFuture> future = null;
		for (T task : taskList) {
			future = threadPool.submit(task);
			// 将任务执行结果存储到List中
			resultList.add(future);
		}
		// 遍历任务的结果
		for (Future<IFuture> fs : resultList) {
			EncryFuture ef;
			try {
				ef = (EncryFuture) fs.get();
				//Log.i("filetask", "file id:" + ef.sourceName);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
			}
			mhandler.sendEmptyMessage(COUNTING);
		}
		// 启动一次顺序关闭，执行以前提交的任务，但不接受新任务。如果已经关闭，则调用没有其他作用。
		// threadPool.shutdown();
		try {
			safeboxDao.batchInsertEncryFileInfo(resultList);
			mhandler.sendEmptyMessage(TASK_FINISH);
		} catch (InterruptedException e) {
			mhandler.sendEmptyMessage(INSERT_ERROR);
		} catch (ExecutionException e) {
			mhandler.sendEmptyMessage(INSERT_ERROR);
		} finally {
			freelize();
		}
	}

	private static FileHandleThreadPool<Callable<IFuture>> mInstance = null;

	public static FileHandleThreadPool<Callable<IFuture>> getInstance() {
		if (mInstance == null) {
			synchronized (FileHandleThreadPool.class) {
				if (mInstance == null) {
					mInstance = new FileHandleThreadPool<Callable<IFuture>>();
				}
			}
		}
		return mInstance;
	}

	public void freelize() {
		resultList.clear();
		taskList.clear();
	}
}
